/*
 * JaCoLiB
 * Copyright (C) 2008 Alessandro Serra
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package it.gashale.jacolib.rpc;

import it.gashale.jacolib.core.JacolibError;
import it.gashale.jacolib.json.Serializer;

public class RPCChannel {
	private Serializer serializer;
	private RPCClient client;
	private RPCClientStub client_stub;
	private RPCServerStub server_stub;
	
	private ExecHandler exec_handler;
	private QuitHandler quit_handler;
	private QuitHandler quit_p_handler;
	
	public RPCChannel(Serializer ser) {
		serializer=ser;
		server_stub=new RPCServerStub(serializer);
		client_stub=new RPCClientStub(serializer);
		client_stub.setRPCChannel(this);
	}
	public RPCClient getRPCClient() {
		return client;
	}
	public void setRPCClient(RPCClient v) {
		client=v;
	}
	public RPCClientStub getRPCClientStub() {
		return client_stub;
	}
	public RPCServerStub getRPCServerStub() {
		return server_stub;
	}
	public void setRPCServer(RPCServer v) {
		server_stub.setServer(v);
		v.setRPCChannel(this);
	}
	public ExecHandler getExecHandler() {
		return exec_handler;
	}
	public void setExecHandler(ExecHandler v) {
		exec_handler=v;
	}
	public QuitHandler getQuitHandler() {
		return quit_handler;
	}
	public void setQuitHandler(QuitHandler v) {
		quit_handler=v;
	}
	public QuitHandler getQuitPHandler() {
		return quit_p_handler;
	}
	public void setQuitPHandler(QuitHandler v) {
		quit_p_handler=v;
	}
	/*
	 * exec
	 */
	public String exec(String req) throws JacolibError {
		return exec_handler.handles(req);
	}
	/*
	 * quit
	 */
	public void quit() throws JacolibError {
		quit_handler.handles();
		quit_p();
	}
	public void quit_p() throws JacolibError {
		quit_p_handler.handles();
		client.quit_p();
		server_stub.quit_p();
		serializer=null;
		client=null;
		client_stub=null;
		server_stub=null;
		exec_handler=null;
		quit_handler=null;
		quit_p_handler=null;
	}
}
